﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;

namespace Pakxel.Framework
{
   public class UIHelper
   {
      public void ClearControls(Control.ControlCollection Controles)
      {
         foreach (var Ctl in Controles)
         {
            switch (Ctl.GetType().Name)
            {
               case "RadDateTimePicker":
                  ((RadDateTimePicker)Ctl).Value = DateTime.Now; break;
               case "RadSpinEditor":
                  ((RadSpinEditor)Ctl).Value = ((RadSpinEditor)Ctl).Minimum; break;
               case "RadMultiColumnComboBox":
                  if (((RadMultiColumnComboBox)Ctl).MultiColumnComboBoxElement.Rows.Count > 0)
                  {
                     ((RadMultiColumnComboBox)Ctl).MultiColumnComboBoxElement.SelectedIndex = 0;
                  }
                  break;
               case "RadDropDownList":
                  ((RadDropDownList)Ctl).SelectedIndex = 0; break;
               case "RadRadioButton":
                  ((RadRadioButton)Ctl).IsChecked = false; break;
               case "RadTextBox":
                  ((RadTextBox)Ctl).Text = ""; break;
               case "RadGroupBox": ClearControls(((RadGroupBox)Ctl).Controls); break;

            }
         }
      }

      /// <summary>Des/Habilita los controles marcados para su edicion</summary>
      /// <param name="control">Contenedor</param>
      /// <param name="Enable">Indica si se habilitaran o no</param>
      /// <param name="Clear">Indica si se borrara el contenido de los controles</param>
      public void EnableControls(Control Control, bool Enable, bool Clear)
      {
         foreach (Control control in Control.Controls)
         {
            switch (control.GetType().Name)
            {
               case "RadGroupBox":
                  EnableControls(control, Enable, Clear);
                  break;
               case "RadDateTimePicker":
                  control.Enabled = Enable;
                  break;
               case "RadMultiColumnComboBox":
                  control.Enabled = Enable;
                  break;
               case "RadDropDownList":
                  control.Enabled = Enable;
                  break;
               case "RadRadioButton":
                  control.Enabled = Enable;
                  break;
               case "RadTextBox":
                  control.Enabled = Enable;
                  ((RadTextBox)control).ReadOnly = !Enable;
                  break;
               default:
                  break;
            }
            if (control.Controls.Count > 0)
               EnableControls(control, Enable, Clear);
            else if (control.GetType().Name == "RadGridView")
            {
               ((RadGridView)control).AllowAddNewRow = Enable;
               ((RadGridView)control).AllowEditRow = Enable;
               ((RadGridView)control).AllowDeleteRow = Enable;
            }

         }
      }

      public void ClearComboBoxFilter(ref RadMultiColumnComboBox Combo)
      {
         Combo.MultiColumnComboBoxElement.EditorControl.MasterTemplate.FilterDescriptors.Clear();
         Combo.MultiColumnComboBoxElement.EditorControl.FilterDescriptors.Clear();

         RadMultiColumnComboBoxElement ComboCliente = Combo.MultiColumnComboBoxElement;
         ComboCliente.EditorControl.FilterDescriptors.Add(new FilterDescriptor(Combo.DisplayMember, FilterOperator.StartsWith, string.Empty));
         Combo.MultiColumnComboBoxElement.AutoCompleteMode = AutoCompleteMode.None;
         if (Combo.MultiColumnComboBoxElement.Rows.Count > 0)
         {
            Combo.MultiColumnComboBoxElement.SelectedIndex = 0;
            Combo.SelectedIndex = 0;
         }
      }

      public void SetComboBoxFilter(ref RadMultiColumnComboBox Combo, string FieldName, object FieldValue)
      {
         if (Combo.MultiColumnComboBoxElement.Rows.Count > 0)
         {
            Combo.MultiColumnComboBoxElement.EditorControl.MasterTemplate.FilterDescriptors.Clear();
            Combo.MultiColumnComboBoxElement.EditorControl.FilterDescriptors.Clear();

            FilterDescriptor MasterFilter = new FilterDescriptor();
            MasterFilter.PropertyName = FieldName;
            MasterFilter.Operator = FilterOperator.IsEqualTo;
            MasterFilter.Value = FieldValue;
            MasterFilter.IsFilterEditor = true;
            Combo.MultiColumnComboBoxElement.EditorControl.MasterTemplate.FilterDescriptors.Add(MasterFilter);

            FilterDescriptor EditorFilter = new FilterDescriptor();
            EditorFilter.PropertyName = FieldName;
            EditorFilter.Operator = FilterOperator.IsEqualTo;
            EditorFilter.Value = FieldValue;
            EditorFilter.IsFilterEditor = true;
            Combo.MultiColumnComboBoxElement.EditorControl.FilterDescriptors.Add(EditorFilter);
            
         }
      }
   }
}
